﻿using System;
using Firebase.Crashlytics;
using UnityEngine;
using UnityEngine.Diagnostics;

public class CrashlyticsInit : MonoBehaviour
{
    void Start()
    {
        // Initialize Firebase
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
        {
            var dependencyStatus = task.Result;
            if (dependencyStatus == Firebase.DependencyStatus.Available)
            {
                // Create and hold a reference to your FirebaseApp,
                // where app is a Firebase.FirebaseApp property of your application class.
                // Crashlytics will use the DefaultInstance, as well;
                // this ensures that Crashlytics is initialized.
                Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;

                // When this property is set to true, Crashlytics will report all
                // uncaught exceptions as fatal events. This is the recommended behavior.
                Crashlytics.ReportUncaughtExceptionsAsFatal = true;

                // Set a flag here for indicating that your project is ready to use Firebase.
                Debug.Log("Firebase已准备好！！！");
            }
            else
            {
                Debug.LogError(System.String.Format(
                    "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                Debug.LogError("Firebase未准备好！！！");
                // Firebase Unity SDK is not safe to use here.
            }
        });
    }

    private void OnGUI()
    {
        GUI.skin.button.fontSize = Screen.width / 25;


        if (GUI.Button(new Rect(50, 50, 400, 100), "Log Error"))
        {
            Debug.LogError("I Log a Error");
        }

        if (GUI.Button(new Rect(50, 200, 400, 100), "Throw"))
        {
            throw new Exception("I Throw a Exception");
        }

        if (GUI.Button(new Rect(50, 350, 400, 100), "Test Crash"))
        {
            Utils.ForceCrash(ForcedCrashCategory.AccessViolation);
        }
    }
}